package service

import (
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/errcode"
)

func (svc *Service) CreateUserTeam(param *model.CreateUserTeamRequest) error {
	// 判断团队是否是满的状态，如果是则返回添加失败的信息
	teamInfo, err := svc.dao.FindTeamById(param.TeamId)
	if err != nil {
		return err
	}
	if teamInfo.ID < 1 {
		return errcode.ErrorFindTeamRecordFail
	}
	if teamInfo.IsFull { // 团队是否是满的状态
		return errcode.ErrorCreateUserTeamFailTeamIsFull
	}
	// 创建用户和团队的关系
	return svc.dao.CreateUserTeam(param.UserId, param.TeamId, param.UserMsg)
}

func (svc *Service) GetUserTeamUserList(teamId uint32, pager *app.Pager) (int, []*model.UserTeamUserListResponse, *errcode.Error) {
	totalRows, err := svc.dao.CountUserTeamUser(teamId)
	if err != nil {
		global.Logger.Errorf("svc.CountUserTeamUser err: %v", err)
		return 0, nil, errcode.ErrorCountUserTeamUserFail.WithDetails(err.Error())
	}
	list, err := svc.dao.GetUserTeamUserList(teamId, pager.Page, pager.PageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetUserTeamUserList err: %v", err)
		return 0, nil, errcode.ErrorGetUserTeamUserListFail.WithDetails(err.Error())
	}
	return int(totalRows), list, nil
}

func (svc *Service) GetUserTeamTeamList(userId uint32, pager *app.Pager) (int, []*model.UserTeamTeamListResponse, *errcode.Error) {
	totalRows, err := svc.dao.CountUserTeamTeam(userId)
	if err != nil {
		return 0, nil, errcode.ErrorCountUserTeamTeamFail.WithDetails(err.Error())
	}
	list, err := svc.dao.GetUserTeamTeamList(userId, pager.Page, pager.PageSize)
	if err != nil {
		return 0, nil, errcode.ErrorGetUserTeamTeamListFail.WithDetails(err.Error())
	}
	return int(totalRows), list, nil
}

func (svc *Service) GetUTTeamListByTN(param *model.UserTeamTeamListByTeamNameRequest, pager *app.Pager) (int, []*model.UserTeamTeamListResponse, *errcode.Error) {
	totalRows, err := svc.dao.CountUTTeamByTN(param.UserId, param.TeamName)
	if err != nil {
		return 0, nil, errcode.ErrorCountUTTeamByTNFail.WithDetails(err.Error())
	}
	list, err := svc.dao.GetUTTeamListByTN(param.UserId, param.TeamName, pager.Page, pager.PageSize)
	if err != nil {
		return 0, nil, errcode.ErrorGetUTTeamListByTNFail.WithDetails(err.Error())
	}
	return int(totalRows), list, nil
}

func (svc *Service) UserTeamAgree(param *model.UserTeamAgreeRequest) error {
	// 判断该用户下是否有该团队
	userTeamInfo, err := svc.dao.FindUserTeamById(param.ID)
	if err != nil {
		return errcode.ErrorUserTeamFindByIdFail.WithDetails(err.Error())
	}
	teamInfo, err := svc.dao.FindTeamById(userTeamInfo.TeamId)
	if err != nil {
		return errcode.ErrorFindTeamByIdFail.WithDetails(err.Error())
	}
	if teamInfo.CreatedBy != param.UserId {
		return errcode.ErrorUserExcludeTeam
	}
	return svc.dao.UserTeamAgree(param.ID)
}

func (svc *Service) DeleteUserTeam(param *model.UserTeamRefuseRequest) error {
	// 判断该用户下是否有该团队
	userTeamInfo, err := svc.dao.FindUserTeamById(param.ID)
	if err != nil {
		return errcode.ErrorUserTeamFindByIdFail.WithDetails(err.Error())
	}
	teamInfo, err := svc.dao.FindTeamById(userTeamInfo.TeamId)
	if err != nil {
		return errcode.ErrorFindTeamByIdFail.WithDetails(err.Error())
	}
	if teamInfo.CreatedBy != param.UserId {
		return errcode.ErrorUserExcludeTeam
	}
	return svc.dao.DeleteUserTeam(param.ID)
}

func (svc *Service) UserLeaveTeam(param *model.UserLeaveTeamRequest) error {
	// 查找该用户和团队的关系记录
	ut, err := svc.dao.FindUserTeamByUT(param.UserId, param.TeamId)
	if err != nil {
		return err
	}
	if ut.ID < 1 {
		return errcode.ErrorFindUserTeamRecordFail
	}
	return svc.dao.DeleteUserTeam(ut.ID)
}
